#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mydialog.h"

#include <QDebug>
#include <QMessageBox>
#include <QString>
#include <QTimer>
#include <QFileDialog>
#include <QFontDialog>
#include <QInputDialog>
#include <QProgressDialog>
#include <QAction>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    // renew
    ui->setupUi(this);

    // 设置窗口的的最大尺寸
    setMaximumSize(800, 800);
    // 设置最小尺寸
    setMinimumSize(200, 200);
    // 设置固定尺寸
    // setFixedSize(1920, 1080);
    // 设置窗口标题
    setWindowTitle("TestWindow");
    // 设置窗口图标
    setWindowIcon(QIcon("D:\\Coder\\pictures\\icons\\调试.png"));

    // 将改变信息 打印到终端
    connect(this, &MainWindow::windowTitleChanged, this, [=]()
    {
        qDebug() << "标题改变: " << windowTitle();
    });

    connect(this, &MainWindow::windowIconChanged, this, [=]()
    {
        qDebug() << "窗口的图标被修改";
    });

    // 设置窗口右键菜单策略
    setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, &MainWindow::customContextMenuRequested, this, [=](const QPoint& pos){
        QMenu menu;
        menu.addAction("Copy");
        menu.addAction("Paste");
        menu.addAction("Delete");
        menu.exec(QCursor::pos());
    });

    ui->fontLabel->setStyleSheet("border: 1px solid black;");

//    connect(ui->build_action, &QAction::triggered, this, [=]()
//    {

//    });

    ui->toolBar->addWidget(new QPushButton("搜索"));

    QLineEdit* edit = new QLineEdit;
    // edit->setFixedWidth(400); // 设置固定宽度 400
    edit->setMaximumWidth(600); // 设置最大宽度

    ui->toolBar->addWidget(edit);

    // 添加第二个工具栏
    QToolBar* toolbar = new QToolBar("second toolbar");
    this->addToolBar(Qt::RightToolBarArea, toolbar); // 在窗口右侧位置

    // 状态栏添加子控件
    ui->statusbar->showMessage("我是状态栏");
    QPushButton* button = new QPushButton("按钮");
    ui->statusbar->addWidget(button);
    // 标签
    QLabel* label = new QLabel("贴一个标签");
    ui->statusbar->addWidget(label);

    QTimer::singleShot(5000, this, [=]() // singleShot() 在指定的延迟时间后执行一次特定的操作
    {
        button->show();
        label->show();
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_moveBtn_clicked()
{
    QRect rect = this->frameGeometry();
    // 从左上角开始向右下角移动
    move(rect.topLeft() + QPoint(10, 20));
}

void MainWindow::on_posBtn_clicked()
{
    QRect rect = this->frameGeometry();
    // 获取位置信息
    qDebug() << "左上角-> " << rect.topLeft()
             << "右上角-> " << rect.topRight()
             << "左下角-> " << rect.bottomLeft()
             << "右下角-> " << rect.bottomRight()
             << "高度: " << rect.height()
             << "宽度: " << rect.width();
}

void MainWindow::on_geometryBtn_clicked()
{
    srand(time(NULL));
    int x = rand() % 800;
    int y = rand() % 800;
    int width = this->width() + rand()%151-50;
    int height = this->height() + rand()%151-50;
    setGeometry(x, y, width, height);
}

void MainWindow::on_modifyBtn_clicked()
{
    QString currentTitle = windowTitle();
    if (currentTitle == "TestWindow") {
        setWindowTitle("Title Changed");
        setWindowIcon(QIcon("D:\\Coder\\pictures\\icons\\瞄准_aiming.png"));
    } else {
        setWindowTitle("TestWindow");
        setWindowIcon(QIcon("D:\\Coder\\pictures\\icons\\调试.png"));
    }
}

void MainWindow::on_modalDlg_clicked()
{
    MyDialog dlg;

    // 输出结果信息到终端
    connect(&dlg, &MyDialog::finished, this, [=](int ret)
    {
       qDebug() << "result: " << ret;
    });
    connect(&dlg, &MyDialog::accepted, this, [=]()
    {
       qDebug() << "accept signal has been transmitted.";
    });
    connect(&dlg, &MyDialog::rejected, this, [=]()
    {
       qDebug() << "reject signal has been transmitted.";
    });

    int ret = dlg.exec();
    if(ret == QDialog::Accepted)
        qDebug() << "accept button clicked ...";
    else if(ret == QDialog::Rejected)
        qDebug() << "reject button clicked ...";
    else
        qDebug() << "done button clicked ...";
}

void MainWindow::on_msgbox_clicked()
{

    QMessageBox::about(this, "about", "这是一个消息提示框");
    QMessageBox::critical(this, "critical", "这是一个错误对话框");
    int ret = QMessageBox::question(this, "question", "是否保存修改的内容"
                                    ,QMessageBox::Save|QMessageBox::Cancel, QMessageBox::Cancel);
    if(ret == QMessageBox::Save){
        QMessageBox::information(this, "information", "保存成功");
    }else if(ret == QMessageBox::Cancel){
        QMessageBox::information(this, "information", "退出成功，未保存");
    }

}

#define OPENDIR
void MainWindow::on_filedlg_clicked()
{
#ifdef OPENDIR
    QString dirName = QFileDialog::getExistingDirectory(this,"打开目录", "D:\Coder\Warehouse\QT");
    QMessageBox::information(this, "打开目录", "目录位置" + dirName);

#endif

#ifdef OPENFILE
    QString arg("Text files (x.txt)") ;
    QString fileName = QFileDialog::getopenFileName(
                this, tr("Open File"), "D:\Coder\Warehouse\QT",
                tr("Images (*.png *.jpg);;Text files (*.txt)"), &arg);
    QMessageBox::information(this, "打开文件", "您选择的文件是: " + fileName);
#endif

#ifdef OPENFILES
    QStringList fileNames = QFileDialog::getOpenFilesNames(
                this, tr("Open Files", "D:\Coder\Warehouse\QT"),
                tr("Images (*.png *.jpg);;Text files (*.txt)"));
    QMessageBox::information(this, "打开文件(s)", "您选择的文件有: " + fileNames)
#endif

#ifdef SAVEFILE
    QString fileName = QFileDialog::getSaveFileName(
                this, "保存文件", "D:\Coder\Warehouse\QT");
    QMessageBox::information(this, "保存文件: ", "所保存的数据文件是:" + fileName);
#endif
}

void MainWindow::on_fontdlg_clicked()
{
#if true
    bool ok;
    QFont ft = QFontDialog::getFont(&ok, QFont("微软雅黑", 12, QFont::Bold), this, "选择字体");
    qDebug() << "ok value is " << ok;
#else
    QFont ft = QFontDialog::getFont(NULL);
#endif
    ui->fontLabel->setFont(ft);
}

#define INT
void MainWindow::on_inputdlg_clicked()
{
#ifdef INT
    // 数字参数分别代表: 默认值，最小值，最大值，step
    int ret = QInputDialog::getInt(this, "年龄", "输入当前年龄:", 18, 1, 100, 2);
    QMessageBox::information(this, "年龄", "您的当前年龄:" + QString::number(ret));
#endif

#ifdef DOUBLE
    double ret = QInputDialog::getDouble(this, "工资", "输入预期工资: ", 10000, 2800, 30000, 100);
    QMessageBox::information(this, "工资", "您的预期工资", QString::number(ret));
#endif

#ifdef ITEM
    QString items;
    items << "跑步" << "哑铃" << "游泳" << "自由训练" << "骑车" << "引体";
    QString item = QInputDialog::getItem(this, "选择喜好运动", "你的喜好运动: ", items, 1, false);
    QMessageBox::information(this, "运动", "你的喜好运动: ", item);
#endif

#ifdef TEXT
    QString text = QInputDialog(this, "密码", "输入密码: ", QLineEdit::Password, "114514");
    QMessageBox::information(this, "密码", "您的密码是: ", text);
#endif

#ifdef MULTITEXT
    QString info = QInputDialog(this, "好友", "输入要发送的信息", "吃了吗");
    QMessageBox::information(this, "好友", "发送了信息: " + info);
#endif
}

void MainWindow::on_progressdlg_clicked()
{
    // 1. 创建进度条对话框
    QProgressDialog *progress = new QProgressDialog(
                "正在更新...", "取消更新", 0 , 100 , this);
    // 2. 初始化并显示进度条窗口
    progress->setWindowTitle("请稍后");
    // progress->setWindowModality(Qt::WindowModal); // 设为模态对话框
    progress->show();

    // 3. 更新进度条
    static int value = 0;
    QTimer* timer = new QTimer(this);

    connect(timer, &QTimer::timeout, this, [=]()
    {
        progress->setValue(value++);
        // progress->setValue(progress->value() + 1);

        // 当value > 最大值时
        if(value > progress->maximum())
        {
            timer->stop();
            value = 0;
            delete progress;
            delete timer;
        }
    });

    connect(progress, &QProgressDialog::canceled, this, [=]()
    {
        timer->stop();
        value = 0;
        delete progress;
        delete timer;
    });

    timer->start(50);
}
